import 'dart:io';

import 'package:card_topia/api/network_util.dart';
import 'package:card_topia/common_provider/page_provider.dart';
import 'package:card_topia/common_provider/user_provider.dart';
import 'package:card_topia/my_router.dart';
import 'package:card_topia/pages/community_page/community_page.dart';
import 'package:card_topia/pages/home_page/home_page.dart';
import 'package:card_topia/pages/message_page/message_page.dart';
import 'package:card_topia/pages/profile_page/profile_page.dart';
import 'package:card_topia/utils/shared_preference_util.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'design_specifications.dart';
import 'my_bottom_navigation_bar.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SPUtil.init();
  //初始化网络工具
  NetworkUtil.init(env: Env.prod);
  //透明状态栏
  if (Platform.isAndroid) {
    SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
        statusBarColor: Colors.transparent,
        // statusBarIconBrightness: Brightness.dark,
        systemNavigationBarColor: scaffoldBackgroundColor));
  }
  runApp(const ProviderScope(child: MyApp()));
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      designSize: const Size(393, 852),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (context, child) => MaterialApp(
        key: MyRouter.globalKey,
        routes: MyRouter.routes,
        initialRoute: MyRouter.root,
        title: '乐咔',
        debugShowCheckedModeBanner: false,
        builder: EasyLoading.init(),
        theme: ThemeData(
          colorScheme: ColorScheme.fromSeed(seedColor: themeColor),
          splashColor: Colors.transparent,
          highlightColor: Colors.transparent,
          scaffoldBackgroundColor: scaffoldBackgroundColor,
        ),
        supportedLocales: const [
          Locale('zh', 'CN'), // 中文简体
        ],
        localizationsDelegates: const [
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
      ),
      child: const MyHomePage(),
    );
  }
}

class MyHomePage extends ConsumerStatefulWidget {
  const MyHomePage({
    super.key,
  });

  @override
  ConsumerState<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends ConsumerState<MyHomePage> {
  late final userPod = ref.read(userInfoProvider);
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      MyRouter.setContext(context);
    });
    //初始化用户数据
    Future.delayed(Duration.zero,()=>initUserData());
  }

  initUserData() {
    userPod.loadSP();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Column(
        children: [
          Expanded(
              child: IndexedStack(
            index: ref.watch(pageProvider).currentPage,
            children: [
              const CommunityPage(),
              const ChowkPage(),
              Container(),
              const MessagePage(),
              const ProfilePage(),
            ],
          )),

        ],
      ),
      bottomNavigationBar: const MyBottomNavigationBar(),
      // floatingActionButton: const MyBottomNavigationBar(),
      // floatingActionButtonLocation: FloatingActionButtonLocation.centerFloat,
    );
  }
}
